//
//  JEHRightTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/5.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHRightTableViewCell.h"

@interface JEHRightTableViewCell ()

@property (nonatomic, retain) UILabel *introLabel;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) JEHDataBaseHandle *handle;

@end

@implementation JEHRightTableViewCell

- (void)dealloc {
    
    _delegate = nil;
    [_pictureView release];
    [_introLabel release];
    [_button release];
    [_titleLabel release];
    [_nameLabel release];
    [_rootModel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self createView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.pictureView.frame = CGRectMake(10, 15, self.height - 30, self.height - 30);
    self.introLabel.frame = CGRectMake(self.pictureView.right + 10, self.height - 30, self.width / 2, 30);
    self.titleLabel.frame = CGRectMake(self.pictureView.right + 10, 0, self.width - 20 - self.pictureView.width, self.height);
    self.nameLabel.frame = CGRectMake(self.pictureView.right + 10, 10, self.width / 2, self.height / 2);
    self.button.frame = CGRectMake(self.width - 60, self.height / 2 - 10, 50, 20);
}

- (void)setRootModel:(JEHRootModel *)rootModel {
    if (_rootModel != rootModel) {
        [_rootModel release];
        _rootModel = [rootModel retain];
    }
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:rootModel.kpic] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    _pictureView.contentMode = UIViewContentModeScaleAspectFill;
    _pictureView.clipsToBounds = YES;
    self.introLabel.text = rootModel.shortIntro;
    self.nameLabel.text = rootModel.name;
    if (!rootModel.name) {
        self.titleLabel.text = rootModel.title;
        self.button.alpha = 0;
        _button.userInteractionEnabled = NO;
    } else {
        self.titleLabel.text = nil;
        _button.userInteractionEnabled = YES;
        self.button.alpha = 1;
    }
    
    self.handle = [JEHDataBaseHandle shareDataBaseHandle];
    [self.handle openDataBase];
    if ([self.handle selectWithName:rootModel.name]) {
        
        [self.button setTitle:@"已订阅" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _button.layer.borderColor = [UIColor grayColor].CGColor;
    } else {
        [self.button setTitle:@"订阅" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _button.layer.borderColor = [UIColor redColor].CGColor;
    }
    [self.handle closeDataBase];
}

- (void)setTemp:(NSInteger)temp {
    if (_temp != temp) {
        _temp = temp;
    }
}

- (void)createView {
    
    self.pictureView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_pictureView release];
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_nameLabel release];
    _nameLabel.font = [UIFont systemFontOfSize:15];
    self.introLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_introLabel release];
    _introLabel.textColor = [UIColor grayColor];
    _introLabel.font = [UIFont systemFontOfSize:10];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_titleLabel release];
    _titleLabel.numberOfLines = 0;
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.layer.borderWidth = 0.4;
    [_button addTarget:self action:@selector(subscribeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_pictureView];
    [self addSubview:_titleLabel];
    [self addSubview:_nameLabel];
    [self addSubview:_introLabel];
    [self addSubview:_button];
}

- (void)subscribeButtonAction:(UIButton *)sender {

    self.handle = [JEHDataBaseHandle shareDataBaseHandle];
    [_handle openDataBase];
    if ([self.handle selectWithName:self.rootModel.name]) {
        
        [self.button setTitle:@"订阅" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _button.layer.borderColor = [UIColor redColor].CGColor;
        [_handle deleteTitleArrayFromDataBase:self.rootModel.name];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"取消订阅成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alert show];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [alert dismissWithClickedButtonIndex:0 animated:NO];
        });
        [alert release];
    } else {
        [self.button setTitle:@"已订阅" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _button.layer.borderColor = [UIColor grayColor].CGColor;
        [_handle insertTitleArrayIntoDataBase:self.rootModel.name withUrl:self.rootModel.url];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"订阅成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alert show];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [alert dismissWithClickedButtonIndex:0 animated:NO];
        });
        [alert release];
    }
    [_handle closeDataBase];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"ReloadData" object:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
